package com.wenjie;

import java.util.Stack;

import com.util.TreeNode;

public class KthNode047 {
    int count = 0;
    TreeNode KthNode(TreeNode pRoot, int k)
    {
        if(count > k || pRoot == null)
            return null;
        TreeNode p = pRoot;
        Stack<TreeNode> LDRStack = new Stack<TreeNode>();
        TreeNode kthNode = null;
        while(p != null || !LDRStack.isEmpty()){
            while(p != null){
                LDRStack.push(p);
                p = p.left;
            }
            TreeNode node = LDRStack.pop();
            System.out.print(node.val+",");
            count++;
            if(count == k){
                kthNode = node;
            }
            p = node.right;
        }
        return kthNode;
    }
}
